package com.tancw.cloud.payment.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.tancw.cloud.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付控制器
 */
@Slf4j
@RestController
@RequestMapping
public class PaymentController {

    @PostMapping("/pay")
    @SentinelResource(value = "createPayment")
    public Result<Map<String, Object>> createPayment(@RequestBody Map<String, Object> paymentReq) {
        Long orderId = Long.valueOf(paymentReq.get("orderId").toString());
        BigDecimal amount = new BigDecimal(paymentReq.get("amount").toString());
        
        log.info("创建支付: orderId={}, amount={}", orderId, amount);

        // TODO: 调用第三方支付接口
        
        Map<String, Object> result = new HashMap<>();
        result.put("paymentNo", "PAY" + System.currentTimeMillis());
        result.put("orderId", orderId);
        result.put("amount", amount);
        result.put("status", "SUCCESS");
        
        return Result.success(result);
    }

    @GetMapping("/query/{paymentNo}")
    public Result<Map<String, Object>> queryPayment(@PathVariable String paymentNo) {
        log.info("查询支付: {}", paymentNo);
        
        Map<String, Object> result = new HashMap<>();
        result.put("paymentNo", paymentNo);
        result.put("status", "SUCCESS");
        
        return Result.success(result);
    }

    @PostMapping("/refund")
    @SentinelResource(value = "refundPayment")
    public Result<Map<String, Object>> refundPayment(@RequestBody Map<String, Object> refundReq) {
        String paymentNo = refundReq.get("paymentNo").toString();
        BigDecimal amount = new BigDecimal(refundReq.get("amount").toString());
        
        log.info("退款: paymentNo={}, amount={}", paymentNo, amount);

        Map<String, Object> result = new HashMap<>();
        result.put("refundNo", "REF" + System.currentTimeMillis());
        result.put("paymentNo", paymentNo);
        result.put("amount", amount);
        result.put("status", "SUCCESS");
        
        return Result.success(result);
    }
}
